package com.yanggeng.demo.beginnerguidedemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by yanggeng on 2017/8/3.
 */

public class GradientProgressView extends View {

    private Paint linePaint;
    private Paint textPaint;
    private Paint numTextPaint;

    private int red, green, blue, width, height, lineWidth;
    private int[] colors = {Color.parseColor("#00FF00"), Color.parseColor("#FFFF00"), Color.parseColor("#FF0000")};

    private String[] upText = {"较差", "一般", "良好", "优秀", "极好"};

    private String[] underText = {"400", "500", "600", "700", "800", "900"};

    private float length = 100;

    private float currentPosition = 0;

    private float targetPosition = 0; //目标距离（0—100）

    private Context context;

    private int xOffset = 50;

    private int state = 2; // 进度条运动状态 1前进 2后退，默认后退

    private boolean isRunning;

    public void show(final float position) {
        if (isRunning) {
            return;
        }
        targetPosition = position;
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                switch (state) {
                    case 1:
                        currentPosition += 2;
                        if (currentPosition >= position) {
                            currentPosition = position;
                            state = 2;
                            timer.cancel();
                        }
                        break;

                    case 2:
                        currentPosition -= 2;
                        if (currentPosition <= 0) {
                            currentPosition = 0;
                            state = 1;
                        }
                        break;
                }
                postInvalidate();
            }
        }, 300, 50);
    }


    public GradientProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        linePaint = new Paint();
        linePaint.setColor(Color.WHITE);
        linePaint.setARGB(255, 255, 255, 255);
        linePaint.setAntiAlias(true);
        linePaint.setStyle(Paint.Style.FILL);

        textPaint = new Paint();
        textPaint.setColor(Color.BLUE);
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(sp2px(context, 16));

        numTextPaint = new Paint();
        numTextPaint.setARGB(255, 255, 255, 255);
        numTextPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        lineWidth = width - xOffset * 2;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        drawLine(canvas);
        drawStaticLine(canvas);
//        drawUpText(canvas);
//        drawUnderText(canvas);
    }

    private void drawUpText(Canvas canvas) {
        float half = (float) lineWidth / 10;
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(sp2px(context, 16));
        for (int i = 0; i < 5; i++) {
            canvas.drawText(upText[i], ((i * 2) + 1) * half + xOffset, 60, textPaint);
        }
    }


    private void drawStaticLine(Canvas canvas) {
//        linePaint.setStrokeWidth(dip2px(context, 3));
        LinearGradient lg;
        float end = lineWidth + xOffset;
        lg = new LinearGradient(xOffset, 100, end, 100, colors, null, Shader.TileMode.REPEAT);

        linePaint.setShader(lg);
//        canvas.drawLine(xOffset, 100, end, 100, linePaint);
        canvas.drawRoundRect(xOffset, 100, end, 150, 25, 25, linePaint);

        float arrowX = currentPosition / 100 * lineWidth + xOffset;

        Path path = new Path();
        path.moveTo(arrowX - 20, 100);
        path.lineTo(arrowX, 75);
        path.lineTo(arrowX + 20, 100);
        path.lineTo(arrowX - 20, 100);
        path.close();
        canvas.drawPath(path, linePaint);

        canvas.drawText(String.valueOf(currentPosition), arrowX, 75, textPaint);
//        Paint paint = new Paint();
//        paint.setColor(Color.GRAY);
//        paint.setStrokeWidth(dip2px(context, 3));
//        canvas.drawLine(end, 100, lineWidth + xOffset, 100, paint);
    }

    private void drawLine(Canvas canvas) {
        linePaint.setStrokeWidth(dip2px(context, 3));
        for (int i = 0; i < currentPosition; i++) {
//            if(currentPosition <= 50) {
//                red = 235 + (int) (i * 0.4);
//                green = 111 + (int)(83 * i / 50f);
//                blue = 111 - (int)(76 * i / 50f);
//            } else {
//                red = 255 - (int)(195 * (i - 51) / 50f);
//                green = 194 + (int)(17 * (i -51) / 50f);
//                blue = 35 + (int) (88 * (i - 51)/ 50f);
//            }
            float percent = i / length;
            red = 235 - (int) (175 * percent);
            green = 111 + (int) (100 * percent);
            blue = 80 + (int) (43 * percent);
            float start = ((float) (i) / 100) * lineWidth + xOffset;
            float end = ((float) (i + 1) / 100) * lineWidth + xOffset;

            linePaint.setARGB(255, red, green, blue);

            canvas.drawLine(start, 100, end, 100, linePaint);
        }
        linePaint.setColor(Color.GRAY);
        float start = ((float) (currentPosition) / 100) * lineWidth + xOffset;
        canvas.drawLine(start, 100, lineWidth + xOffset, 100, linePaint);
    }

    private void drawUnderText(Canvas canvas) {
        float half = (float) lineWidth / 5;
//        textPaint.setTextAlign(Paint.Align.CENTER);
        numTextPaint.setColor(Color.BLACK);
        numTextPaint.setTextSize(sp2px(context, 16));
        for (int i = 0; i < 6; i++) {
            canvas.drawText(underText[i], half * i + xOffset, 160, textPaint);
        }
    }

    public void setCurrentPosition(float currentLength) {
        this.targetPosition = currentLength;
        this.currentPosition = currentLength;
        postInvalidate();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private int sp2px(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public int getColor() {
        float percent = targetPosition / length;
        red = 235 - (int) (175 * percent);
        green = 111 + (int) (100 * percent);
        blue = 80 + (int) (43 * percent);

        return Color.rgb(red, green, blue);
    }
}
